package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.mes.biz.domain.BizOrder;
import com.mes.biz.domain.BizOrderInstaller;
import com.mes.biz.service.BizOrderService;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizMeasure;
import com.mes.biz.service.BizMeasureService;

/**
 * 测量单Controller
 * @author swf
 * @email swf
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/biz/bizMeasure")
public class BizMeasureController extends BaseController {

    @Autowired
    private BizMeasureService bizMeasureService;
    @Autowired
    private BizOrderService bizOrderService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询测量单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizMeasure bizMeasure, HttpServletRequest request, HttpServletResponse response) {
        bizMeasure.setPage(new PageDomain(request, response));
        PageInfo<BizMeasure> page = bizMeasureService.findPage(bizMeasure);
//        for (BizMeasure measure : page.getList()) {
//            if (StringUtils.isNotEmpty(measure.getOrderId())){
//                BizOrder bizOrder = bizOrderService.get(measure.getOrderId());
//                if (null != bizOrder){
//                    measure.setOrderNumber(bizOrder.getOrderNumber());
//                }
//            }
//        }
        return R.data(page);
    }

    /**
     * 获取测量单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:query')")
    @GetMapping(value = "/{id}")
    public R<BizMeasure> detail(@PathVariable("id") String id) {
        return R.data(bizMeasureService.get(id));
    }

    /**
     * 新增测量单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:add')")
    @Log(title = "测量单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizMeasure bizMeasure) {
        return R.status(bizMeasureService.create(bizMeasure));
    }

    /**
     * 修改测量单
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:edit')")
    @Log(title = "测量单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizMeasure bizMeasure) {
        return R.status(bizMeasureService.update(bizMeasure));
    }

    /**
     * 删除测量单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:remove')")
    @Log(title = "测量单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizMeasureService.deleteBizMeasureByIds(ids));
    }


    /**
     * 导出测量单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMeasure:export')")
    @Log(title = "测量单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizMeasure bizMeasure) {
        List<BizMeasure> list = bizMeasureService.findList(bizMeasure);
        ExcelUtil<BizMeasure> util = new ExcelUtil<BizMeasure>(BizMeasure.class);
        return util.exportExcel(list, "测量单数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @GetMapping("/appletList")
    public R<PageInfo> appletList(BizMeasure bizMeasure, HttpServletRequest request, HttpServletResponse response) {
        bizMeasure.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        bizMeasure.setMeasurer(loginUser.getUserId());
        PageInfo<BizMeasure> page = bizMeasureService.appletList(bizMeasure);
        return R.data(page);
    }

    @Log(title = "测量单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMeasureStatus")
    public R updateMeasureStatus(@RequestBody BizMeasure bizMeasure) {
        return R.status(bizMeasureService.update(bizMeasure));
    }

    @Log(title = "测量单", businessType = BusinessType.INSERT)
    @PostMapping("/appletAdd")
    public R appletAdd(@RequestBody @Validated  BizMeasure bizMeasure) {
        return R.status(bizMeasureService.create(bizMeasure));
    }
}
